package com.ghost.UI
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import spark.primitives.Rect;

	/**
	 * @author ghost
	 * 绑定非文本滚动条
	 */
	public class Ghost_mc_scroll extends Sprite
	{
		private var up_arrow_btn:Ghost_Btn;//向上箭头；
		private var down_arrow_btn:Ghost_Btn;//向下箭头；
		private var track_btn:Ghost_Btn;//轨道
		private var thumb_btn:Ghost_Btn;//滚动条
		private var scroll_rec:Rectangle;//滚动条滚动范围
		private var scroll_y:Number;//滚动条的y值
		private var o_height:Number;//目标的高
		private var target_obj:DisplayObject;//绑定的目标
		private var scroll_min_y:Number;//滚动条的最小y值
		private var scroll_max_y:Number;//滚动条的最大y值
		private var mouse_wheel_bool:Boolean;//是否鼠标滚动
		private var view_track_height:Number;//滚动条可移动的轨道高
		private var rec:Rectangle;//目标的最新rectangle
		private var thumb_sprite:Sprite;//存放滚动条按钮的容器 
		private var thumb_sprite_bg:Sprite;//thumb_sprite的背景 来控制thumb_sprite的宽高 button控制不了容器的宽高
		/**
		 * @param skin_v 滚动条皮肤数据
		 */		
		public function Ghost_mc_scroll(skin_v:Vector.<Object>=null)
		{
			thumb_sprite = new Sprite();
			thumb_sprite_bg = new Sprite();
			thumb_sprite_bg.graphics.beginFill(0xffffff,0);
			thumb_sprite_bg.graphics.drawRect(0,0,5,5);
			thumb_sprite_bg.graphics.endFill();
			thumb_sprite.addChild(thumb_sprite_bg)
			this.visible = false;
			up_arrow_btn = new Ghost_Btn(skin_v.slice(0,4));
			//up_arrow_btn.label = "";
			
			down_arrow_btn = new Ghost_Btn(skin_v.slice(4,8));
			//down_arrow_btn.label = "";
			track_btn = new Ghost_Btn(skin_v.slice(8,12));
			//track_btn.label = "";
			thumb_btn = new Ghost_Btn(skin_v.slice(12,16));
			//thumb_btn.label = "";
			up_arrow_btn.setsize(14,14);
			down_arrow_btn.setsize(14,14);
			track_btn.setsize(14,14);
			thumb_btn.setsize(14,6);
			thumb_sprite.addChild(thumb_btn);
			this.addChild(track_btn);
			this.addChild(up_arrow_btn);
			this.addChild(down_arrow_btn);
			this.addChild(thumb_sprite);
		}
		/**
		 * @param target_o绑定目标
		 * 滚动条绑定到目标
		 */		
		public function set target(target_o:DisplayObject):void
		{
			target_obj = target_o;
			rec = target_o.scrollRect;
			o_height = target_o.height;
			track_btn.height = rec.height;
			view_track_height = track_btn.height-up_arrow_btn.height-down_arrow_btn.height;
			scroll_min_y = thumb_sprite.y = scroll_y = up_arrow_btn.height;
			down_arrow_btn.y = rec.height-down_arrow_btn.height;
			thumb_btn.height=thumb_sprite_bg.height = view_track_height*(rec.height/o_height);
			trace(thumb_sprite_bg.height,thumb_btn.height,thumb_sprite.height);
			scroll_rec = new Rectangle(0,up_arrow_btn.height,0,view_track_height-thumb_sprite.height);
			scroll_max_y = track_btn.height-down_arrow_btn.height-thumb_sprite.height;
			if(rec!=null && (o_height>rec.height))
			{
				this.visible = true;
				up_arrow_btn.addEventListener(MouseEvent.CLICK,up_cHandler);
				down_arrow_btn.addEventListener(MouseEvent.CLICK,down_cHandler);
				thumb_sprite.addEventListener(MouseEvent.MOUSE_DOWN,thumb_down_Handler);
				thumb_sprite.addEventListener(MouseEvent.MOUSE_UP,thumb_up_Handler);
				thumb_sprite.addEventListener(MouseEvent.MOUSE_OUT,thumb_out_Handler);
			}
			else
			{
				this.visible = false;
			}
		}
		//更新 
		public function update():void
		{
			thumb_sprite.y = up_arrow_btn.height;
			rec.y = 0;
			target_obj.scrollRect = rec;
			thumb_btn.height=thumb_sprite_bg.height = view_track_height*(rec.height/target_obj.height);
			scroll_max_y = track_btn.height-down_arrow_btn.height-thumb_sprite.height;
			if(target_obj.height>rec.height)this.visible = true;
			else this.visible = false;
		}
		//点击向上按钮
		private function up_cHandler(e:MouseEvent):void
		{
			scroll_y--;
			change_rec();
		}
		//点击向下按钮
		private function down_cHandler(e:MouseEvent):void
		{
			scroll_y++;
			change_rec();
		}
		private function change_rec():void
		{
			if(scroll_y<scroll_min_y)
			{
				scroll_y = scroll_min_y;
			}
			else if(scroll_y>scroll_max_y)
			{
				scroll_y = scroll_max_y;
			}
			thumb_sprite.y = scroll_y;
			rec = target_obj.scrollRect;
			rec.y = o_height*((scroll_y-up_arrow_btn.height)/view_track_height);
			target_obj.scrollRect = rec;
		}
		//按下滚动条 开始拖动
		private function thumb_down_Handler(e:MouseEvent):void
		{
			thumb_sprite.startDrag(false,scroll_rec);
			thumb_sprite.addEventListener(Event.ENTER_FRAME,thumb_enter_Handler);
		}
		//侦听拖动 
		private function thumb_enter_Handler(e:Event):void
		{
			scroll_y = thumb_sprite.y;
			change_rec();
		}
		//放开滚动条 停止拖动
		private function thumb_up_Handler(e:MouseEvent):void
		{
			thumb_sprite.stopDrag();
			thumb_sprite.removeEventListener(Event.ENTER_FRAME,thumb_enter_Handler);
		}
		//移出滚动条 停止拖动
		private function thumb_out_Handler(e:MouseEvent):void
		{
			thumb_sprite.stopDrag();
			thumb_sprite.removeEventListener(Event.ENTER_FRAME,thumb_enter_Handler);
		}
		//设置是否支持鼠标滚动
		public function set mouse_wheel(bool:Boolean):void
		{
			if(bool)
			{
				target_obj.addEventListener(MouseEvent.MOUSE_OVER,over_Handler);
				target_obj.addEventListener(MouseEvent.MOUSE_OUT,out_Handler);
				target_obj.addEventListener(MouseEvent.MOUSE_WHEEL,wheel_Handler);
			}
		}
		//移入目标
		private function over_Handler(e:MouseEvent):void
		{
			mouse_wheel_bool = true;
		}
		//移出目标
		private function out_Handler(e:MouseEvent):void
		{
			mouse_wheel_bool = false;
		}
		//鼠标滚动
		private function wheel_Handler(e:MouseEvent):void
		{
			if(mouse_wheel_bool)
			{
				if(e.delta>0)scroll_y--;
				else scroll_y++;
				change_rec();
			}
		}
		/**
		 * @param x
		 * @param y
		 * 将此组件移动到此父容器中的坐标
		 */		
		public function move_to(x:Number,y:Number):void
		{
			this.x = x;
			this.y = y;
		}
	}
}